package com.srp.common.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Iterator;

public class XmlParser {

    public static HashMap<String, Object> parse(String xml) {
        Document doc = null;
        HashMap<String, Object> data = new HashMap<>();
        try {
            doc = DocumentHelper.parseText(xml);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();// 指向根节点

        Iterator it = root.elementIterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();// 一个Item节点
            data.put(element.getName(), element.getTextTrim());

        }
        return data;
    }
}
